package 剑指offer;

import java.util.Arrays;

public class _29顺时针打印矩阵 {
	public static void main(String[] args) {
		int max[][]= {{1,2,3},{4,5,6},{7,8,9}};
		System.out.println(Arrays.toString(max));
	}
	/**
	 * 
	 *@time 2021年2月6日下午9:08:55
	 * 
	 *@param matrix
	 *@return int[]
	 *
	 *@see
	 */
	 public static int[] spiralOrder(int[][] matrix) {
		 if(matrix==null||matrix.length==0||matrix[0].length==0) return new int[0];
		 int n=matrix.length,m=matrix[0].length;
		 int []arr =new int[n*m];
		 int index=0,t=0,r=m-1,b=n-1,l=0; 
		 while (true) {
			for (int i = l; i <=r; i++) arr[index++]=matrix[t][i];
			if(++t>b) break;
			for (int i=t; i <= b; i++) arr[index++]=matrix[i][r];
			if(l>--r) break;
			for (int i = r; i>=l; i--) arr[index++]=matrix[b][i];
			if(t>--b) break;
			for (int i = b; i>=t ; i--) arr[index++]=matrix[i][l]; 
		    if(++l>r)break;			
			
		 }
		 return arr;
		 
	    }

}
